package com.szholly.pro.watergas_server.ps.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.ExceedComparisonService;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

import net.sf.json.JSONObject;
@Controller
@RequestMapping("/ExceedComparison/*")
public class OperationExceedComparisonController extends BaseController{ 
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	ExceedComparisonService exceedComparisonService;
	
	/**
	 * 获取设备部件/耗材更换记录
	 * wangl
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getExceedComparisonList")
	public void getExceedComparisonList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
	
		Map<String,String> params  = getParams();
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = exceedComparisonService.getExceedComparisonList(params, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		String result = json.toString();
		outString(result);
		
		
	}
	
	private Map<String,String> getParams(){
		Map<String, String> params = new HashMap<String, String>();

		String strTASK_ID = RequestUnit.getString("TASK_ID");
		if (StringUtil.IsNotNullOrEmpty(strTASK_ID)) {
			params.put("task_code", strTASK_ID);
		}
		String strWWTP_ID = RequestUnit.getString("WWTP_ID");
		if (StringUtil.IsNotNullOrEmpty(strWWTP_ID)) {
			params.put("wwtp_id", strWWTP_ID);
		}
		String strFLOW_NAME = RequestUnit.getString("FLOW_NAME");
		if (StringUtil.IsNotNullOrEmpty(strFLOW_NAME)) {
			params.put("flow_type", strFLOW_NAME);
		}
		String strSTATION_ID = RequestUnit.getString("STATION_ID");
		if (StringUtil.IsNotNullOrEmpty(strSTATION_ID)) {
			params.put("station_id", strSTATION_ID);
		}
		String water_type = RequestUnit.getString("WATER_TYPE");
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			params.put("water_type", water_type);
		}
		String strSTATION_NAME = RequestUnit.getString("STATION_NAME");
		if (StringUtil.IsNotNullOrEmpty(strSTATION_NAME)) {
			params.put("station_name", strSTATION_NAME);
		}
		String strMEASURE_ITEM = RequestUnit.getString("MEASURE_ITEM");
		if (StringUtil.IsNotNullOrEmpty(strMEASURE_ITEM)) {
			String mi = StringUtil.joinForSQL(strMEASURE_ITEM.split(","), ",");
			params.put("measure_items", "(" + mi + ")");
		}
		String strMEASURE_PERSON = RequestUnit.getString("MEASURE_PERSON");
		if (StringUtil.IsNotNullOrEmpty(strMEASURE_PERSON)) {
			params.put("measure_person", strMEASURE_PERSON);
		}
		String strMEASURE_CONCLUSION = RequestUnit.getString("MEASURE_CONCLUSION");
		if (StringUtil.IsNotNullOrEmpty(strMEASURE_CONCLUSION)) {
			params.put("measure_conclusion", strMEASURE_CONCLUSION);
		}
		String strMEASURE_START_TIME = RequestUnit.getString("MEASURE_START_TIME");
		if (StringUtil.IsNotNullOrEmpty(strMEASURE_START_TIME)) {
			params.put("start", strMEASURE_START_TIME);
		}
		String strMEASURE_END_TIME = RequestUnit.getString("MEASURE_END_TIME");
		if (StringUtil.IsNotNullOrEmpty(strMEASURE_END_TIME)) {
			params.put("end", strMEASURE_END_TIME);
		}
		return params;
	}
	
	/**
	 * 导出超标比对记录表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportDoc")
	public void exportDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		Map<String,String> params = getParams();

		Map<String, Object> root = new HashMap<String, Object>();
		List<RowBase> list = exceedComparisonService.getExceedComparisonList(params, DataUtil.getPageInfo());
		root.put("list", list);
		RowBase row = null;
		if(list!=null && list.size()>0){
			row = list.get(0);
			Date date = (Date)row.getValue("MEASURE_END_TIME");
			root.put("DATE", date);
			root.put("USER", row.getSafeString("MEASURE_PERSON_NAME"));
		}
		String OUNAME = "";
		if (params.containsKey("station_id")) {
			OUNAME = commonService.getOperationUnitByStaionId(params.get("station_id"));
		} else if (params.containsKey("wwtp_id")) {
			OUNAME = commonService.getOperationUnit(params.get("wwtp_id"));
		}
		root.put("OUNAME", OUNAME);
		
		if (params.containsKey("station_id")) {
			row = commonService.getStationGroupInfo(params.get("station_id"));
			if (row != null) {
				root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
			}
			root.put("STATION_NAME", params.get("station_name"));
		} else if (params.containsKey("wwtp_id") && params.containsKey("flow_type")) {
			row = commonService.getStation(params.get("wwtp_id"), params.get("flow_type"));
			if(row!=null){
				String stationName = row.getSafeString("NAME");
				String stationId = row.getSafeString("CODE");
				root.put("STATION_NAME",stationName);
				row = commonService.getStationGroupInfo(stationId);
				if(row!=null){
					root.put("GROUP_LEADER",row.getSafeString("GROUP_LEADER"));
				}
			}
		}
		outWord(root, "overproofCompaire.xml", "超标比对记录表.doc");
	}
	
	/**
	 * 获取字典项
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDataDomain")
	public void getJobList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strDomainName = RequestUnit.getString("DomainName");
		String strSQL = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME='"+strDomainName+"' ";
		String result = DataUtil.getPageSelectJson(strSQL, null);
		outString(result);
	}
}
